package com.example.addressdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.addressdemo.Address.Contact;
import com.example.addressdemo.DB.DBAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 十田田 on 2021/5/30.
 */

public class SearchActivity extends AppCompatActivity {
    private DBAdapter dbAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        dbAdapter=new DBAdapter(this);
        final EditText search_name=(EditText) findViewById(R.id.search_name);
        Button ok_search=(Button) findViewById(R.id.ok_search);
        ImageButton left = (ImageButton) findViewById(R.id.search_left);
        final ListView listView=(ListView) findViewById(R.id.searchlist);
        final List<String> list=new ArrayList<String>();
        final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);


       ok_search.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                list.clear();
                String name=search_name.getText().toString();
                Log.d("ListView1:",name);
                final Contact[] contacts=dbAdapter.getOneData(name);
                for(int i=0;i<contacts.length;i++){
                    list.add(contacts[i].name);
                    Log.d("ListView:",contacts[i].name);
                }

                listView.setAdapter(adapter);

                AdapterView.OnItemClickListener listViewListener=new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?>arg0, View arg1,int position,long arg3){
                        Bundle bundleSimple = new Bundle();
                        bundleSimple.putString("name", contacts[position].name);
                        bundleSimple.putString("phone", contacts[position].phone);
                        bundleSimple.putString("work", contacts[position].work);
                        bundleSimple.putString("home", contacts[position].home);

                        Intent intent=new Intent(SearchActivity.this,DetailActivity.class);
                        intent.putExtras(bundleSimple);
                        startActivity(intent);
                    }
                };
                listView.setOnItemClickListener(listViewListener);
            }
        });



        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SearchActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });






    }
}
